import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.sql.Types;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;

import javax.lang.model.type.ArrayType;

public class JavaGeneric {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String classname;
		/* get class name */
		if (args.length > 0) {
			classname = args[0];
		} else {
			Scanner in = new Scanner(System.in);
			System.out
					.println("Please input a class name (e.g., java.util.Collections):");
			classname = in.next();
		}
		/* print generic info for class and public method */
		try {
			Class cl = Class.forName(classname);
			printClass(cl);
			int n = 0;
			for (Method m : cl.getDeclaredMethods()) {
				n++;
				System.out.print("[" + n + "] ");
				printMethod(m);
			}
		} catch (ClassNotFoundException e) {
			// TODO: handle exception
			System.err.println("Class not found Error: " + e);
		}
	}

	private static void printMethod(Method m) {
		// TODO Auto-generated method stub
		String name = m.getName();
		System.out.print(Modifier.toString(m.getModifiers()));
		System.out.print(" ");
		printTypes(m.getTypeParameters(), "<", ", ", ">", true);
		
		printType(m.getGenericReturnType(), false);
		System.out.print(" ");
		System.out.print(name);
		System.out.print("(");
		printTypes(m.getGenericParameterTypes(), "", ", ", "", false);
		System.out.println(")");
	}

	private static void printClass(Class cl) {
		// TODO Auto-generated method stub

		System.out.print(cl);
		printTypes(cl.getTypeParameters(), "<", ", ", ">", true);
		Type t = cl.getGenericSuperclass();
		if (t != null) {
			System.out.print(" extends ");
			printType(t, false);
		}
		printTypes(cl.getGenericInterfaces(), " implements ", ", ", "", false);
		System.out.println();
	}

	private static void printType(Type t, boolean b) {
		// TODO Auto-generated method stub
		if (t instanceof Class) {
//			System.out.println("\nClass Name!!!");
			Class cl = (Class) t;
			System.out.print(cl.getName());
		} else if (t instanceof TypeVariable) {
			TypeVariable type = (TypeVariable) t;
			System.out.print(type.getName());
			if (b) {
				printTypes(type.getBounds(), " extends ", " & ", "", false);
			}
		} else if (t instanceof WildcardType) {
			WildcardType type = (WildcardType) t;
			System.out.print("?");
			printTypes(type.getUpperBounds(), " extends ", " & ", "", false);
			printTypes(type.getLowerBounds(), " super ", " & ", "", false);
		} else if (t instanceof ParameterizedType) {
			ParameterizedType type = (ParameterizedType) t;
			Type owner = type.getOwnerType();
			if (owner != null) {
				// top-level
				printType(owner, false);
				System.out.print(".");
			}
			printType(type.getRawType(), false);
			printTypes(type.getActualTypeArguments(), "<", ", ", ">", false);
		} else if (t instanceof GenericArrayType) {
			GenericArrayType type = (GenericArrayType) t;
			printType(type.getGenericComponentType(), b);
			System.out.print("[]");
		}
		else if (t instanceof ArrayType) {
			System.out.println("ArrayType"); //this will not happen.
		}
	}

	private static void printTypes(Type[] types, String pre, String sep,
			String suf, boolean isDefinition) {
		// TODO Auto-generated method stub
		if (pre.equals(" extends ")
				&& Arrays.equals(types, new Type[] { Object.class })) {
			return;
		}
		if (types.length > 0) {
			System.out.print(pre);
			printType(types[0], isDefinition);
		}
		for (int i = 1; i < types.length; i++) {
			System.out.print(sep);
			printType(types[i], isDefinition);
		}
		if (types.length > 0) {
			System.out.print(suf);
		}
	}

}
